<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 图书管理
*/
class BookController extends Controller{
	public $book;
	public $cate;
	public $borrow;
	public function __construct(){
		parent::__construct();
        if ($_COOKIE['adminname'] == NULL) {
            $this->error('请登录','/bookManage/admin/index/login',1);
        }
		$this->cate = D('cate');
		$this->book = D('Book');
		$this->borrow = D('borrow');
	}

	//新书入库
	public function addBook(){
		if (IS_POST) {
			$_POST['addtime'] = time();
			if ($this->book->add(I('post.'))) {
				$this->redirect('book/addBook');
			}else{
				$this->error($this->book->getError());
			}
		}
		$this->assign('cate',$this->cate->select());
		$this->display();
	}

	//图书列表
	public function listBook(){
		if (IS_POST) {
			$this->assign('listbook',$this->book->where("b_name like '%".I('post.b_name')."%'")->select());
			$this->display();
		}else{
			$this->assign('listbook',$this->book->order('addtime desc')->limit('10')->select());
			$this->display();
		}
	}

	//删除图书
	public function delBook(){
		if (IS_GET) {
			if ($this->book->where('id='.I('get.id'))->delete()) {
				$this->redirect('book/listbook');
			}else{
				$this->error($this->book->getError());
			}
		}
	}

	//借书
	public function borrowBook(){
		if (IS_POST) {
			$_POST['addtime'] = time();
			$_POST['sign'] = 1;
			$b_name = $_POST['b_name'];
			$pname = $_POST['pname'];
			$info = $this->book->where("b_name='".$b_name."'")->find();
			if (empty($info)) {
				$this->error('该书不存在!','/bookManage/admin/book/borrowBook',2);
			}
			$info = D('user')->where("username='".$pname."'")->find();
			if (empty($info)) {
				$this->error('该用户不存在!','/bookManage/admin/book/borrowBook',2);
			}
			if ($this->borrow->add(I('post.'))) {
				$this->redirect('book/borrowbook');
			}else{
				$this->error($this->borrow->getError());
			}
		}
		$this->display();
	}

	//还书列表
	public function backList(){
		if (IS_GET) {
			$backlist = $this->borrow->where("pname='".I('get.pname')."' and sign=1")->select();
			$time = time();
			foreach ($backlist as $k => $v) {
				$v['lasttime'] = $v['addtime'] + $v['day'] * 86400;
				if ($time <= $v['lasttime']) {
					$v['mark'] = 0;
					$v['residue'] = ceil(($v['lasttime'] - $time) / 86400);
					$backlist[$k] = $v;
				}else{
					$v['mark'] = 1;
					$v['residue'] = floor(($time - $v['lasttime']) / 86400);
					$backlist[$k] = $v;
				}
			}
			$this->assign('backlist',$backlist);
			$this->display();
		}else{
			$this->display();
		}
	}

	//还书操作
	public function backBook(){
		if (IS_GET) {
			$data['sign'] = 0;
			$data['backtime'] = time();
			if ($this->borrow->where('id='.I('get.id'))->save($data)) {
				$this->redirect('book/backList',array('pname'=>I('get.pname')));
			}else{
				$this->error($this->borrow->getError());
			}
		}		
	}
	
	//添加分类
	public function addCate(){
		if (IS_POST) {
			$cateinfo = D('Cate');
			if ($cateinfo->add(I('post.'))) {
				$this->redirect('book/listCate');
			}else{
				$this->error($cateinfo->getError());
			}
		}
		
		$this->display();
	}

	//分类列表
	public function listCate(){
		$cateinfo = D('Cate');
		$this->assign('cateinfo',$cateinfo->select());
		$this->display();
	}

	//删除分类
	public function delCate(){
		if ($this->cate->delete(I('get.id'))) {
			$this->redirect('admin/book/listCate');
		}else{
			$this->error($this->cate->getError());
		}
	}
}